package com.wdb.modules.security.service.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.wdb.modules.system.domain.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * UserDetails 实现
 *
 * @author: wendaobai@qq.com
 * @date: 2024/1/16 0016 10:06
 */
@Data
@AllArgsConstructor
public class JwtUserDto implements UserDetails {

  private final User user;

  private final List<Long> dataScopes;

  private final List<AuthorityDto> authorities;

  public Set<String> getRoles() {
    return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());
  }

  @Override
  @JSONField(serialize = false)
  public String getPassword() {
    return user.getPassword();
  }

  @Override
  @JSONField(serialize = false)
  public String getUsername() {
    return user.getUsername();
  }

  @JSONField(serialize = false)
  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  @JSONField(serialize = false)
  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @JSONField(serialize = false)
  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  @Override
  @JSONField(serialize = false)
  public boolean isEnabled() {
    return user.getEnabled();
  }
}
